﻿using God.ShoppingWebApi.DTO;
using God.ShoppingWebApi.Entities;
using God.ShoppingWebApi.IService;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace God.ShoppingWebApi.Service
{
    /// <summary>
    /// 权限
    /// </summary>
  public  class AdminPermissionSerive: ApplicationService, IAdminPermissionSerive
    {
        IRepository<Permission, Guid> _permisssion;
        public AdminPermissionSerive(IRepository<Permission, Guid> permisssion)
        {
            _permisssion = permisssion;
        }
        [HttpPost]
        [Route("api/PermissionAdd")]
        /// <summary>
        /// 权限添加
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        public async Task AddAsync(PermissionDTO permission)
        {
            Permission ci = new Permission(GuidGenerator.Create(), permission.Title,
                permission.Action,
                permission.Status,
                permission.Create_time,
                permission.Update_time,
                permission.ParentId,
                permission.APIAction,
                permission.IsMenu,
                permission.Title,
                CurrentUser.Id);

            await _permisssion.InsertAsync(ci);
        }
        [Route("api/PermissionDel")]
        /// <summary>
        /// 权限删除
        /// </summary>
        /// <param name="gu"></param>
        /// <returns></returns>

        public async Task DeleteAsync(Guid gu)
        {
            await _permisssion.DeleteAsync(gu);
        }
        [HttpGet]
        [Route("api/PermissionEdit")]
        /// <summary>
        /// 权限反填
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        public async Task<PermissionDTO> Edit(Guid gid)
        {
            var data = await _permisssion.GetAsync(gid);
            var PermissionDTO = ObjectMapper.Map<Permission, PermissionDTO>(data);
            return PermissionDTO;
        }
        [Route("api/PermissionShow")]
        /// <summary>
        /// 权限显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<PermissionDTO>> GetListAsync()
        {
            var str = await _permisssion.GetListAsync();

            return ObjectMapper.Map<List<Permission>, List<PermissionDTO>>(str);

        }
        [Route("api/PermissionUpdate")]
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="permissionDTO"></param>
        /// <returns></returns>
        public async Task<PermissionDTO> Update(PermissionDTO permissionDTO)
        {
            var data = await _permisssion.GetAsync(permissionDTO.Id);
            if (data != null)
            {
                data.LastModificationTime = DateTime.Now;
                data.Title = permissionDTO.Title;
                data.Action = permissionDTO.Action;
                data.Status = permissionDTO.Status;
                data.Create_time = permissionDTO.Create_time;
                data.Update_time = permissionDTO.Update_time;
                data.ParentId = permissionDTO.ParentId;
                data.APIAction = permissionDTO.APIAction;
                data.IsMenu = permissionDTO.IsMenu;
                var Info = await _permisssion.UpdateAsync(data);

                return permissionDTO;
            }
            else
            {
                return null;
            }
        }
    }
}
